package com.gqzdev.bean;

import java.util.StringJoiner;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.stereotype.Component;

/**
 * Aware接口，帮我们装配Spring底层的一些组件
 *
 * @author tanghailan
 * @create 2021年09月01日 22:11
 */
@Component
public class Department implements ApplicationContextAware, MessageSourceAware {

	public Department() {
		System.out.println("创建对象。。。。。。");
	}

	//@Autowired
	ApplicationContext context;

	MessageSource messageSource;

	public ApplicationContext getContext() {
		return context;
	}


	private String name;

	//@Autowired
	private Cat cat;

	@Lookup //去容器中找 @Bean的这种方式注册的Person @Lookup不生效
	public Cat getCat() {
		return cat;
	}

	public void setCat(Cat cat) {
		this.cat = cat;
	}

	@Override
	public String toString() {
		return new StringJoiner(", ", Department.class.getSimpleName() + "[", "]")
				.add("name='" + name + "'")
				.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		//回调机制,把ioc容器传入
		this.context = applicationContext;

	}

	@Override
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
}
